'''
字典
'''


# dict()
# stus = {
#     "key1":"value1",
#     "key2":"value2",
#     "key3":"value3"
# }
# 定义字典
stus = {
    "张三":("张三","黑河",20),
    "李四":("李四","南京",19),
    "王五":("王五","黑河",18),
    "赵六":("赵六","黑河",22),
}
print(stus)
print(stus["赵六"])
print(stus["王五"])

namelist = ["张三","李四","王五"]
agelist = [  22 ,  30  ,50]



for name,age in zip(namelist,agelist):
    print(name,age)

dictionary = dict(zip(namelist, agelist))

print("dictionary",dictionary)


person = dict(name="张三",age=18,address="黑龙江")
print("person",person)

# 从list生成 字段，
adict = dict.fromkeys(['name', 'age', 'sex'])
print("adict",adict)
# None


# 获取字典的数据
print('person["age"]:',person["age"])

# 异常的处理，如果key不存在则抛出异常 可以通过try expect语句处理
try:
    print('person["abc"]:',person["abc"])
except KeyError:
    pass


name = person.get("name");
print("name : ",name)

abc = person.get("abc","default 没有找到");
print("abc : ",abc)

# 对字典的遍历
# list = ["a","b","c","d"]
# for char in list:
#     print("char:",char)

person = dict(name="张三",age=18,address="黑龙江")
print(type(person.items()))
for item in person.items():
    # print(type(item))
    #  (   key    ,  value)
    print(item[0],item[1])


print("======================遍历key==================")
# 遍历key
for key in person.keys():
    print(key,person.get(key))


print("======================遍历value ==================")
for value in person.values():
    print(value)

print("======================元素的添加==================")
aDict = {}
aDict['address'] = 'SDIBT'
print("aDict",aDict)

print("======================元素的更新==================")
aDict.update({'name':'jshand','age':18})
print("aDict",aDict)

print("======================元素的更新2==================")
aDict.update({'name':'jshand2','age':180})
print("aDict",aDict)


print("======================setdefault==================")
aDict.setdefault("gender","男")
print("aDict",aDict)

print("======================第二次setDefault==================")
aDict.setdefault("gender","第二次setDefault")
print("aDict",aDict)

print("aDict.__len__() ",aDict.__len__())
print("======================clear之后==================")
aDict.clear()
print("aDict: ",aDict)
print("aDict.__len__() ",aDict.__len__())



aDict = {"name":"张飞","age":18}
print("aDict : ",aDict)

print('aDict.pop("name") : ',aDict.pop("name"))
print("aDict : ",aDict)


print("======================popitem==================")
aDict = {"name":"张飞","age":18}
print("aDict : ",aDict)
t1 = aDict.popitem()
print("t1: ",t1)
print("aDict : ",aDict)

t2 = aDict.popitem()
print("t2: ",t2)
print("aDict : ",aDict)


print("======================判断是否存在key ==================")
aDict = {"name":"张飞","age":18}
print('"name" in aDict.keys():  ',"name" in aDict.keys())
print('"abc" in aDict.keys():  ',"abc" in aDict.keys())


for key in aDict:
    print("key: ",key,"value: ",aDict[key])

# 字典的推导式
namelist = ["张三","李四","王五"]
agelist = [  22 ,  30  ,50]
mydict = {}
for name,age in zip(namelist,agelist):
    mydict[name]  = age
print("mydict : ",mydict)

mydict2 = { name:age for name,age in zip(namelist,agelist) }
print("mydict2 : ",mydict2)